﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CApAPPActiveCollisionClearancePair
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System.ComponentModel;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands;

internal class CApAPPActiveCollisionClearancePair : INotifyPropertyChanged
{
  private TxCollisionPair _collisionPair;
  private double _desiredClearance = -1.0;
  private const double MAX_VALUE = 10000.0;
  private const string APP_DESIRED_CLEARANCE_ATTRIBUTE_NAME = "APP_DESIRED_CLEARANCE";

  public string CollisionSetName
  {
    get
    {
      return TxCollisionPair.op_Equality(this._collisionPair, (ITxObject) null) ? string.Empty : this._collisionPair.Name;
    }
  }

  public double NearMiss
  {
    get
    {
      if (TxCollisionPair.op_Equality(this._collisionPair, (ITxObject) null))
        return -1.0;
      return this._collisionPair.IsNearMissDefined ? this._collisionPair.NearMissDistance / TxApplication.Options.Units.LinearMultiplier : 0.0;
    }
  }

  public double DesiredClearance
  {
    get => this._desiredClearance / TxApplication.Options.Units.LinearMultiplier;
    set
    {
      this._desiredClearance = value * TxApplication.Options.Units.LinearMultiplier;
      this.OnPropertyChanges(nameof (DesiredClearance));
    }
  }

  public double MaximumDesiredClearance => 10000.0 / TxApplication.Options.Units.LinearMultiplier;

  public double MinimumDesiredClearance => this.NearMiss;

  public string NumericLinearFormat => CApAPPActiveCollisionClearanceUtilities.NumericLinearFormat;

  public CApAPPActiveCollisionClearancePair(TxCollisionPair collisionPair)
  {
    this._collisionPair = collisionPair;
    this.DesiredClearance = this.NearMiss;
    this.LoadAttribute();
  }

  public void Update()
  {
    if (TxDoubleExtension.AlmostEquals(this._desiredClearance, this._collisionPair.NearMissDistance))
      this._collisionPair.RemoveAttribute("APP_DESIRED_CLEARANCE");
    else
      this.StoreAttribute();
  }

  public void RestoreDefaults() => this.DesiredClearance = this.NearMiss;

  private void LoadAttribute()
  {
    if (!TxCollisionPair.op_Inequality(this._collisionPair, (ITxObject) null) || !(this._collisionPair.GetAttribute("APP_DESIRED_CLEARANCE") is TxDoubleAttribute attribute))
      return;
    this._desiredClearance = attribute.Value;
  }

  private void StoreAttribute()
  {
    if (!TxCollisionPair.op_Inequality(this._collisionPair, (ITxObject) null))
      return;
    this._collisionPair.SetAttribute((TxAttribute) new TxDoubleAttribute("APP_DESIRED_CLEARANCE", this._desiredClearance));
  }

  public event PropertyChangedEventHandler PropertyChanged;

  protected virtual void OnPropertyChanges(string propertyName)
  {
    if (this.PropertyChanged == null)
      return;
    this.PropertyChanged((object) this, new PropertyChangedEventArgs(propertyName));
  }
}
